package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    //登录
    @PostMapping("/employee/login")
    public ResultInfo login(HttpSession session,@RequestBody Map<String,String> map) {
        //1.接收参数
        String username = map.get("username");
        String password = map.get("password");
        //2.调用Service登录
        ResultInfo resultInfo=employeeService.login(username,password);
        //3.将查询的用户信息存储到session
        if(resultInfo.getCode()==1) {
            Employee employee=(Employee) resultInfo.getData();
            session.setAttribute("SESSION_EMPLOYEE", employee);
        }
        //4.返回ResultInfo
        return resultInfo;
    }

    //退出
    @PostMapping("/employee/logout")
    public ResultInfo logout(HttpSession session) {
        //1.清理Session的用户信息
        session.invalidate();
        //2.返回成功标识
        return ResultInfo.success(null);
    }
    //列表查询
    @GetMapping("/employee/find")
    public ResultInfo findList(String name) {
        //1.调用service
        List<Employee> employeeList=employeeService.findByName(name);
        //2.返回结果
        return  ResultInfo.success(employeeList);
    }

    //保存
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee) {
        employeeService.save(employee);
        return ResultInfo.success(null);
    }
    //根据id查询员工
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable("id") Long id) {
        Employee employee=employeeService.findById(id);
        return ResultInfo.success(employee);
    }

    //修改员工
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee) {
        employeeService.update(employee);
        return ResultInfo.success(null);
    }

}
